//
//  HMEmoticonModel.swift
//  表情键盘
//
//  Created by iOS on 16/9/28.
//  Copyright © 2016年 iOS. All rights reserved.
//

import UIKit

/// 一个表情模型
class HMEmoticonModel: NSObject, NSCoding {
    /// 图片所在的文件夹名称
    var id: String?
    
    /// 表情名称
    var chs: String?
    
    /// 图片名称
    var png: String? {
        didSet {
            // 图片的完整路径 = bundlePath + 文件夹的名称 + 图片名称
            if let id = id, let png = png {
                fullPngPath = bundlePath + "/" + id + "/" + png
            }
        }
    }
    
    /// 图片的完整路径
    var fullPngPath: String?
    
    /// emoji的16进制字符串
    var code: String? {
        didSet {
            
            // 扫描器
            let scanner = Scanner(string: code!)
            
            // result: UnsafeMutablePointer<UInt32>?: 不安全的可变的 UInt32 类型的指针
            var result: UInt32 = 0
            scanner.scanHexInt32(&result)
            
//            print("result: \(result)")
            
            emoji = String(Character(UnicodeScalar(result)!))
        }
    }
    
    var emoji: String?
    
    init(id: String, dict: [String: String]) {
        self.id = id
        super.init()
        
        self.setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    /// 打印
    override var description: String {
        get {
            let keys = ["chs", "png", "code"]
            return "\n \t\t 表情模型: \(dictionaryWithValues(forKeys: keys))"
        }
    }
    
    /// 将模型的图片转成属性文本
    func toAttributedText(font: UIFont) -> NSAttributedString {
        guard let fullPngPath = self.fullPngPath else {
            print("没有图片路径")
            return NSAttributedString()
        }
        
        // 1.生成图片
        let image = UIImage(named: fullPngPath)
        
        // 图片是不能直接添加到textView文本中的.和发邮件一样可以使用附件(属性文本)
        // 2.将图片转成附件
        let attachment = HMTextAttachment()
        attachment.image = image
        attachment.chs = chs
        let height = font.lineHeight
        // 设置附件大小
        // frame 往下移动 设置y 为正数
        // bounds 和 frame相反 往下移动 设置y 为负数
        attachment.bounds = CGRect(x: 0, y: -3, width: height, height: height)
        
        // 3.将附件转成属性文本
        let attributeText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
        // 3.0.1 给附件添加font属性
        attributeText.addAttribute(NSFontAttributeName, value: font, range: NSRange(location: 0, length: 1))
        
        return attributeText
    }
    
    // MARK: - 归档和解档
    required init?(coder aDecoder: NSCoder) {
        id = aDecoder.decodeObject(forKey: "id") as? String
        chs = aDecoder.decodeObject(forKey: "chs") as? String
        
        // 在init方法里面赋值是不会触发didSet
        png = aDecoder.decodeObject(forKey: "png") as? String
        // 因为每次运行程序的路径会变化,记录图片的完整路径后,下次启动找不到图片了.
//        fullPngPath = aDecoder.decodeObject(forKey: "fullPngPath") as? String
        code = aDecoder.decodeObject(forKey: "code") as? String
        emoji = aDecoder.decodeObject(forKey: "emoji") as? String
        
        // 图片的完整路径 = bundlePath + 文件夹的名称 + 图片名称
        if let id = id, let png = png {
            fullPngPath = bundlePath + "/" + id + "/" + png
        }
    }
    
    func encode(with aCoder: NSCoder) {
        aCoder.encode(id, forKey: "id")
        aCoder.encode(chs, forKey: "chs")
        aCoder.encode(png, forKey: "png")
        aCoder.encode(fullPngPath, forKey: "fullPngPath")
        aCoder.encode(code, forKey: "code")
        aCoder.encode(emoji, forKey: "emoji")
    }
    
    /// 根据名称找到模型
    class func emoticonModel(withEmoticonName name: String) -> HMEmoticonModel? {
        // 遍历所有的表情包
        for package in HMEmoticonManager.shared.packages {
            // 遍历包里面的所有表情,查找名称一样的表情
            for emoticon in package.emoticons {
                if emoticon.chs == name {
                    // 找到模型了
                    return emoticon
                }
            }
        }
        
        return nil
    }
}
